 #-*- coding: utf-8 -*-

from openerp import models, fields, api, _

class purchase_apply(models.Model):

    _name = 'purchase.apply'

    dep_id=fields.Many2one('hr.department',required=True,string=u"申请部门")
    date_apply=fields.Datetime(string=u"申请时间",readonly=True,default=fields.Datetime.now())
    applicant_id=fields.Many2one('res.users',string=u"申请人",required=True,readonly=True,default=lambda self: self.env.user)
    apply_line=fields.One2many('purchase.apply.line', 'apply_id', string=u"采购明细")
    compare_user_manager=fields.Boolean(string=u"比较当前用户是否为经理",compute='_get_compare_user_manager')
    state=fields.Selection([
        ('draft','Draf'),
        ('confirmed','Confirmed'),
        ('storeconfirmed','Storeconfirmed'),
        ('waitconfirmed','Waitconfirmed'),
        ('departmentconfirmed','Departmentconfirmed'),
        # ('allconfirmed','Allconfirmed'),
        # ('hrconfirmed','Hrconfirmed'),
        # ('finconfirmed','Finconfirmed'),
        # ('buyconfirmed','Buyconfirmed'),
        # ('techconfirmed','Techconfirmed'),
        # ('sebconfirmed','Sebconfirmed'),
        # ('proconfirmed','Proconfirmed'),
        # ('saleconfirmed','Saleconfirmed'),
        ('purchaseconfirmed','Purchaseconfirmed'),
        ('financialconfirmed', 'Financialconfirmed'),
        ('managerconfirmed','ManagerConfirmed')],default='draft')

    @api.one
    def _get_depart_name(self):
        print(self.dep_id.name)
        return self.dep_id.name

    @api.onchange('applicant_id')
    def _onchange_applicant_id(self):
        if self.applicant_id and self.applicant_id.employee_ids[0]:
            self.dep_id = self.applicant_id.employee_ids[0].department_id
            print(self.dep_id)
    
    @api.one
    @api.depends('dep_id')
    def _get_compare_user_manager(self):
        print('_get_depart_name',self,self.dep_id)
        if self.dep_id:
            dep_manager_id=self.dep_id.manager_id.user_id
            dep_user_id=self.env.user
            print(dep_manager_id,dep_user_id)
            print(dep_manager_id.id==dep_user_id.id)
            self.compare_user_manager =dep_manager_id.id==dep_user_id.id

    @api.one
    def action_draft(self):
        self.write({'state':'draft'})

    @api.one
    def action_confirm(self):
        self.write({'state':'confirmed'})

    @api.one
    def action_storeconfirm(self):
        self.write({'state':'storeconfirmed'})

    @api.one
    def action_waitconfirm(self):
        self.write({'state':'waitconfirmed'})

    @api.one
    def action_departmentconfirm(self):
        self.write({'state':'departmentconfirmed'})

    @api.one
    def action_purchaseconfirm(self):
        self.write({'state':'purchaseconfirmed'})    

    @api.one
    def action_financialconfirm(self):
        self.write({'state':'financialconfirmed'})


    @api.one
    def action_managerconfirm(self):
        self.write({'state':'managerconfirmed'})

class purchase_apply_line(models.Model):

    _name = 'purchase.apply.line'

    production_id=fields.Many2one('product.product',string=u"物品名称")
    production_type=fields.Char(string=u"规格型号")
    production_qty=fields.Float(string=u"数量")
    production_unit=fields.Many2one('product.uom',string=u"单位")
    suggestion=fields.Char(string=u"建议品牌或厂家")
    use_date=fields.Char(string=u"使用日期")
    information=fields.Char(string=u"详细用途")
    store_qty=fields.Char(string=u"目前库存数量")
    apply_id=fields.Many2one('purchase.apply',string=u"采购申请")

     